package com.core.baseDao.extend.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.core.baseDao.BaseDao;
import com.core.baseDao.extend.BaseEntityDao;
import com.core.baseDao.extend.Pager;
import com.core.util.GenericsUtils;

/**
 * Manager基类，调用GenericDAO实现实体的CRUD操作
 * @author
 * @param <T> 实体类型
 * @param <PK> 实体主键类型
 */
public class BaseEntityDaoImpl<T, PK extends Serializable> implements BaseEntityDao<T, PK> {
    /**
     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
     */
    protected final Log log = LogFactory.getLog(getClass());

    protected BaseDao<T,PK> baseDao;
    
    protected Class<T> entityClass;

    @SuppressWarnings("unchecked")
	public BaseEntityDaoImpl()
    {
      this.entityClass = GenericsUtils.getSuperClassGenricType(getClass());
    }
    
	@Override
	public T load(PK id) {
		return this.baseDao.load(entityClass,id);
	}

	@Override
	public T get(PK id) {
		return this.baseDao.get(entityClass,id);
	}

	@Override
	public boolean contains(T t) {
		return this.baseDao.contains(t);
	}

	@Override
	public void refresh(T t) {
		 this.baseDao.refresh(t);
	}

	@Override
	public Serializable save(T t) {
		return this.baseDao.save(t);
	}

	@Override
	public void saveOrUpdate(T t) {
		this.baseDao.saveOrUpdate(t);
	}

	@Override
	public void saveOrUpdateAll(Collection<T> entities) {
		this.baseDao.saveOrUpdateAll(entities);
	}

	@Override
	public void update(T t, LockMode lockMode) {
		this.baseDao.update(t,lockMode);
	}

	@Override
	public void update(T t) {
		this.baseDao.update(t);
	}

	@Override
	public void delete(T t, LockMode lockMode) {
		this.baseDao.delete(t,lockMode);
	}

	@Override
	public void delete(T t) {
		this.baseDao.delete(t);
	}

	@Override
	public void deleteAll(Collection<T> entities) {
		this.baseDao.deleteAll(entities);
	}

	@Override
	public List<T> find(String queryString, Object value) {
		return this.baseDao.find(queryString,value);
	}

	@Override
	public List<T> find(String queryString, Object[] values) {
		return this.baseDao.find(queryString,values);
	}

	@Override
	public List<T> find(String queryString) {
		return this.baseDao.find(queryString);
	}

	@Override
	public List<T> getAll() {
		return this.baseDao.getAll(entityClass);
	}

	@Override
	public List<T> findByNamedQuery(String queryName) {
		return this.baseDao.findByNamedQuery(queryName);
	}

	@Override
	public List<T> findByNamedQuery(String queryName, Object value) {
		return this.baseDao.findByNamedQuery(queryName,value);
	}

	@Override
	public List<T> findByNamedQuery(String queryName, Object[] values) {
		return this.baseDao.findByNamedQuery(queryName,values);
	}

	@Override
	public Object findUnique(String queryString, Object[] values) {
		return this.baseDao.findUnique(queryString,values);
	}

	@Override
	public boolean exists(PK id) {
		return this.baseDao.exists(entityClass,id);
	}

	@Override
	public HibernateTemplate getHibernate() {
		return this.baseDao.getHibernate();
	}

	/**
	 * @return the baseDao
	 */
	public BaseDao<T, PK> getBaseDao() {
		return baseDao;
	}

	/**
	 * @param baseDao the baseDao to set
	 */
	public void setBaseDao(BaseDao<T, PK> baseDao) {
		this.baseDao = baseDao;
	}

	/**
	 * 
	 * 使用hql 语句进行操作
	 * @param hql
	 *            需要执行的hql语句
	 * @param offset
	 *            设置开始位置
	 * @param length
	 *            设置读取数据的记录条数
	 * @return List 返回所需要的集合。
	 */
	public Pager getByPage(final String hql,final Pager pager) {
		return baseDao.getByPage(hql, pager);
	}
}
